/*
 *  Copyright 2009 Utkin Dmitry
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
 
/* 
 *  This file is part of the WSF Staff project.
 *  Please, visit http://code.google.com/p/staff for more information.
 */

// This file generated by staff_codegen
// Service Implementation

#include <staff/security/Security.h>
#include <staff/common/Exception.h>
#include <rise/common/exmacros.h>
#include <rise/common/ExceptionTemplate.h>
#include "LoginContext.h"
#include "LoginImpl.h"

namespace staff
{

  CLoginImpl::~CLoginImpl()
  {
  }

  std::string CLoginImpl::Login(const std::string& sUserName, const std::string& sPassword)
  {
    const std::string& sCurrentSessionId = GetSessionID();
    std::string tResult;
    char szSessionId[33];

    RISE_ASSERTES(sCurrentSessionId == STAFF_SECURITY_GUEST_SESSION_ID,
      rise::CLogicAlreadyExistsException, 
      "Cannot login from non-guest session");
    
    if(!StaffSecurityOpenSession(sUserName.c_str(), sPassword.c_str(), szSessionId, sizeof(szSessionId)))
    {
      RISE_THROWS(staff::CRemoteException, "Cannot open session");
    }

    tResult.assign(szSessionId);

    return tResult;  // result
  }

  std::string CLoginImpl::OpenExtraSession(int nExtraSessionId)
  {
    const std::string& sCurrentSessionId = GetSessionID();
    std::string tResult;
    char szSessionId[33];

    if(!StaffSecurityOpenExtraSession(sCurrentSessionId.c_str(), nExtraSessionId, szSessionId, sizeof(szSessionId)))
    {
      RISE_THROWS(staff::CRemoteException, "Cannot open extra session");
    }

    tResult.assign(szSessionId);

    return tResult;  // result
  }

  void CLoginImpl::Logout()
  {
    const std::string& sCurrentSessionId = GetSessionID();

    if(!StaffSecurityCloseSession(sCurrentSessionId.c_str()))
    {
      RISE_THROWS(staff::CRemoteException, "Cannot close session");
    }
  }

  void CLoginImpl::KeepAliveSession()
  {
    if(!StaffSecurityKeepAliveSession(m_sSessionId.c_str()))
    {
      RISE_THROWS(staff::CRemoteException, "Cannot keepalive session");
    }
  }

  std::string CLoginImpl::GetUserName()
  {
    char szUserName[128];
    std::string tResult;
    if(!StaffSecurityGetUserNameBySessionId(m_sSessionId.c_str(), szUserName, sizeof(szUserName)))
    {
      RISE_THROWS(staff::CRemoteException, "Cannot get current user name");
    }

    tResult.assign(szUserName);

    return tResult;  // result
  }

  int CLoginImpl::GetSessionExpiration() const
  {
    return StaffSecurityGetSessionExpiration();
  }

  bool CLoginImpl::ValidateSession()
  {
    return StaffSecurityValidateSessionID(GetSessionID().c_str());
  }

  const std::string& CLoginImpl::GetSessionID()
  {
    if(m_sSessionId.size() == 0)
    {
      m_sSessionId = CLoginContext::GetContext().GetServiceID(this);
    }
    return m_sSessionId;
  }

}

